package com.manager.common.job;

import com.jcraft.jsch.ChannelSftp;
import com.manager.common.constant.MimicryTcpFileType;
import com.manager.common.ssh.RemoteExecuteLinuxCommandTools;
import com.manager.common.utils.DataTools;
import com.manager.common.utils.LargeFileHttpUploadUtils;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @ClassName IntervalGetStateJsonJob
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025/9/12 13:44
 * @Version 1.0
 */
//@Component
public class IntervalGetStateJsonJobBak {

    @Scheduled(fixedDelay = 5 * 1000)
    public void execute() throws Exception{
       try{
           String remoteDir = "/opt/data";
           List<ChannelSftp.LsEntry> lsEntries = RemoteExecuteLinuxCommandTools.getRemoteLsEntryByDir(remoteDir);
           List<String> fileNameList = new ArrayList<>();
           //stats_1757583906.json
           for (ChannelSftp.LsEntry lsEntry : lsEntries) {
               if(lsEntry.getFilename().equals(".") || lsEntry.getFilename().equals("..") || !lsEntry.getFilename().endsWith(".json")){
                   continue;
               }
               fileNameList.add(lsEntry.getFilename());
           }
           fileNameList.sort(new Comparator<String>() {
               @Override
               public int compare(String o1, String o2) {
                   String dateStr1 = o1.split("_")[1];
                   int date1 = Integer.valueOf(dateStr1.substring(0, dateStr1.lastIndexOf(".")));
                   String dateStr2 = o2.split("_")[1];
                   int date2 = Integer.valueOf(dateStr2.substring(0, dateStr2.lastIndexOf(".")));
                   return date1 - date2;
               }
           });
           System.out.println("==========IntervalGetStateJsonJob==============");
           String mustNewFilePath = "";
           if(fileNameList.size() > 0 ){
               mustNewFilePath = remoteDir + "/" + fileNameList.get(fileNameList.size() - 1);
           }
           if(StringUtils.isEmpty(mustNewFilePath)){
               return;
           }
           List<String> out = new ArrayList<>();
           String localFilePath = LargeFileHttpUploadUtils.getUploadFileSavePath() + "/" + System.currentTimeMillis() + "_" + 6 + ".json";
           boolean flag = RemoteExecuteLinuxCommandTools.downloadFileBySftp(mustNewFilePath,localFilePath,out);

           if(flag){
               String jsonData = FileUtils.readFileToString(new File(localFilePath), "utf-8");
               if(StringUtils.isEmpty(jsonData)){
                   return ;
               }
               JSONObject jsonObject = JSONObject.fromObject(jsonData);
               jsonObject.put("network_sate",1);
               DataTools.putCacheData(MimicryTcpFileType.STATE_COUNT.getEnDesc(), jsonObject);
           }
       }catch (Exception e){
           JSONObject jsonObject = DataTools.getCacheData(MimicryTcpFileType.STATE_COUNT.getEnDesc());
           if(jsonObject != null){
               jsonObject.put("network_sate",0);
               DataTools.putCacheData(MimicryTcpFileType.STATE_COUNT.getEnDesc(), jsonObject);
           }
       }
    }
}


